package cn.interestingshop.web.manager;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.interestingshop.config.AlipayConfig;
import cn.interestingshop.service.baseOrder.BaseOrderService;
import cn.interestingshop.service.baseOrder.BaseOrderServiceImpl;
import com.alipay.api.internal.util.AlipaySignature;

@WebServlet(value = "/NotifyServlet")
public class NotifyServlet extends HttpServlet {



    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        System.out.println("111");



        Map<String, String> map = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); )
        {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++)
            {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }

            map.put(name, valueStr);
        }
        //调用SDK验证签名
        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(
                    map,
                    AlipayConfig.ALIPAY_PUBLIC_KEY,
                    AlipayConfig.CHARSET,
                    AlipayConfig.SIGNTYPE);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        if (signVerified)
        {
            System.out.println(map.toString());
            String out_trade_no=map.get("out_trade_no");

            if ("TRADE_SUCCESS".equals(map.get("trade_status"))) {
                //交易成功
                System.out.println("交易成功");
                //交易逻辑




            }

        }

    }

}